//
//  YFButtonCategoryView.m
//  QuanJieShopProject
//
//  Created by apple on 2018/4/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFButtonCategoryView.h"
#import "YFHomeBtn.h"

@interface YFButtonCategoryView()
{
    CGFloat _padding;
}
@end

@implementation YFButtonCategoryView

// height 在赋值之前就是固定死的
- (YFButtonCategoryView *)initWithOrigin:(CGPoint)origin {
    
    YFButtonCategoryView *view = [[YFButtonCategoryView alloc] init];
    view.mj_origin = origin;
    _padding = UISCREEN_HEIGHT * 30.f/1334.f;
    CGFloat btnW = UISCREEN_WIDTH/5.f;
    CGFloat btnH = btnW * 50.f/44.f; // 之前图片是正方形, 81 * 81
    // 85.f/81.f
    view.height = 2 * btnH + _padding;
    return view;
}

- (void)updateWithTitlesArr:(NSArray *)titlesArr imgsArr:(NSArray *)imgsArr {
    
    CGFloat btnW = UISCREEN_WIDTH/5.f;
    CGFloat btnH = btnW * 50.f/44.f;
    CGFloat btnFirstLineY = _padding; // 第一行的Y值
    NSInteger tag = ZeroTagClothing;
    
    CGFloat viewH = 2 * btnH + _padding; // padding是底部边距

    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, UISCREEN_WIDTH, viewH);
    
    for (NSUInteger i = 0; i < titlesArr.count; i++) {
        YFHomeBtn *button = [YFHomeBtn buttonWithTitle:titlesArr[i] imageName:imgsArr[i] withSize:CGSizeMake(btnW, btnH)];
        button.tag = tag;
        [button addTarget:self action:@selector(headerBtnClickAction:) forControlEvents:UIControlEventTouchUpInside];
        int row = i%5;
        int line = i/5;
        button.mj_x = btnW * row;
        button.mj_y = btnFirstLineY + line *btnH; //
        [self addSubview:button];
//        button.backgroundColor = [UIColor randomColor];
//        button.imageView.backgroundColor = [UIColor redColor];
        tag++;
    }
    
    


}

- (void)headerBtnClickAction:(UIButton *)button {
    
    NSInteger tag = button.tag;
    if (self.buttonClickBlock) {
        self.buttonClickBlock(tag);
    }
}
@end
